package com.cg.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cg.mapper.StockPublicMapper;
import com.cg.po.StockPO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
@RequiredArgsConstructor
public class StockService {

    private final StockPublicMapper stockPublicMapper;

    @Transactional
    public Boolean reduceStockNum(Integer stockId, Integer stockType) {
        StockPO stockPO = stockPublicMapper.selectById(stockId);
        if (Objects.isNull(stockPO)) {
            throw new RuntimeException("当前库存不存在");
        }
        StockPO build = StockPO.builder()
                .stockNum(stockPO.getStockNum() - 1)
                .build();
        return stockPublicMapper.update(build, new LambdaQueryWrapper<StockPO>()
                .eq(StockPO::getStockId, stockId)
                .eq(StockPO::getStockType, stockType)) > 0;
    }

}
